package com.obrock;

import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.*;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 05.10.12
 * Time: 10:11
 * To change this template use File | Settings | File Templates.
 */

public class ViewDetails extends LinearLayout {
    Balance selectedTransfer;
    //int bgColor;
    Context context;

    public ViewDetails(Balance balance, Context context) {
        super(context);
        this.context = context;
        this.selectedTransfer = balance;
        //this.bgColor = color;
        refresh();
    }

    public ViewDetails(Balance balance, Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.selectedTransfer = balance;
        //this.bgColor = color;
        refresh();
    }

    /*
    public ViewDetails(Balance balance, Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        this.selectedTransfer = balance;
        refresh();
    }
    */


    public void refresh(Balance balance) {
        this.selectedTransfer = balance;
        refresh();
    }

    public void refresh() {
        inflate(context, R.layout.details, this);
        //setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        int bgColor = context.getResources().getColor(R.color.pane_color);
        if(Main.viewMode == Main.VIEW_DETAILS) {
            bgColor = context.getResources().getColor(R.color.background_color);
        }
        setBackgroundColor(bgColor);

        if (selectedTransfer == null) return;

        TableLayout complexContainer = (TableLayout) findViewById(R.id.container_complex);
        TableLayout analyticsContainer = (TableLayout) findViewById(R.id.container_analytics);
        LinearLayout blockTo = (LinearLayout) findViewById(R.id.block_to);
        LinearLayout blockComplex = (LinearLayout) findViewById(R.id.block_complex);
        LinearLayout blockTemplate = (LinearLayout) findViewById(R.id.block_template);
        LinearLayout blockAmount = (LinearLayout) findViewById(R.id.block_amount);


        TextView datePlace = (TextView) findViewById(R.id.date_place);
        TextView balanceFrom = (TextView) findViewById(R.id.balance_from);
        TextView balanceTo = (TextView) findViewById(R.id.balance_to);
        TextView transferFrom = (TextView) findViewById(R.id.transfer_from);
        TextView transferTo = (TextView) findViewById(R.id.transfer_to);
        TextView number = (TextView) findViewById(R.id.number);
        TextView key = (TextView) findViewById(R.id.key);
        TextView template = (TextView) findViewById(R.id.template);
        TextView force = (TextView) findViewById(R.id.force);

        TextView remarkTitle = (TextView) findViewById(R.id.remark_title);
        TextView analyticsTitle = (TextView) findViewById(R.id.analytics_title);

        TextView name = (TextView) findViewById(R.id.transfer_name);
        TextView from = (TextView) findViewById(R.id.from);
        TextView to = (TextView) findViewById(R.id.to);
        TextView amount = (TextView) findViewById(R.id.amount);
        TextView remark = (TextView) findViewById(R.id.remark);

        selectedTransfer.refresh();

        if(selectedTransfer.mode == Balance.MODE_TEMPLATE) {
            blockAmount.setVisibility(GONE);
            blockTemplate.setVisibility(VISIBLE);
        } else {
            blockAmount.setVisibility(VISIBLE);
            blockTemplate.setVisibility(GONE);
        }

        if (selectedTransfer.type == Balance.TYPE_COMPLEX) {
            blockComplex.setVisibility(View.VISIBLE);
            blockTo.setVisibility(View.GONE);
        } else {
            blockComplex.setVisibility(View.GONE);
            blockTo.setVisibility(View.VISIBLE);
        }


            name.setText(selectedTransfer.name);
        if (selectedTransfer.accountFrom == null) {
            from.setText(getResources().getString(R.string.transfer_empty));
        } else {
            from.setText(selectedTransfer.accountFrom.name);
        }

        balanceFrom.setText("");

        if (selectedTransfer.accountTo == null) {
            to.setText(getResources().getString(R.string.transfer_empty));
        } else {
            to.setText(selectedTransfer.accountTo.name);
        }

        balanceTo.setText("");

        remark.setText(selectedTransfer.remark);
        datePlace.setText(Main.datetimeFullString(selectedTransfer.date));

        complexContainer.removeAllViews();

        if (selectedTransfer.type != Balance.TYPE_COMPLEX) {
            amount.setText("" + selectedTransfer.amount);
        } else {
            amount.setText("0.00");

            int i = 0;
            for (Complex c : selectedTransfer.complex) {

                TextView ttl = Main.getTextView(context, c.name);
                ttl.setTextSize(12);
                ttl.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

                TextView acc = Main.getTextView(context,
                        c.account == null
                                ? getResources().getString(R.string.transfer_empty)
                                : c.account.name);
                acc.setTextColor(Main.getAccountColor(c.account));
                acc.setTextSize(12);

                LinearLayout col = new LinearLayout(context);
                col.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                col.setOrientation(LinearLayout.VERTICAL);
                col.addView(ttl);
                col.addView(acc);


                TextView amt = Main.getTextView(context, String.format("%.2f", c.amount));
                amt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                amt.setTextSize(18);
                amt.setGravity(Gravity.RIGHT);

                TableRow row = Main.addRow(context, bgColor, i++, new View[]{col, amt});
                complexContainer.addView(row);
            }

            TextView amt = Main.getTextView(context, String.format("%.2f", selectedTransfer.amount));
            amt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            amt.setTextSize(18);
            amt.setTextColor(getResources().getColor(R.color.total_color));
            amt.setGravity(Gravity.RIGHT);
            amt.setPadding(0, 0, 0, 16);

            complexContainer.addView(Main.addRow(context, bgColor, -1, new View[]{
                    Main.getTextView(context, ""),
                    amt
            }));
        }


        analyticsContainer.removeAllViews();

        for (final Analytics section : selectedTransfer.analytics.keySet()) {

            Analytics current = selectedTransfer.analytics.get(section);
            if (current != null) {

                TextView title = Main.getTextView(context, section.name);
                title.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                title.setSingleLine(true);
                title.setEllipsize(TextUtils.TruncateAt.END);
                title.setPadding(0, 0, 8, 0);

                TextView value = Main.getTextView(context, current.name);
                value.setSingleLine(true);
                value.setEllipsize(TextUtils.TruncateAt.END);

                TableRow row = new TableRow(context);
                row.addView(title);
                row.addView(value);

                analyticsContainer.addView(row);
            }
        }

        if (selectedTransfer.remark.length() == 0) {
            remarkTitle.setVisibility(View.GONE);
        } else {
            remarkTitle.setVisibility(View.VISIBLE);
        }

        if (selectedTransfer.analytics.size() == 0) {
            analyticsTitle.setVisibility(View.GONE);
        } else {
            analyticsTitle.setVisibility(View.VISIBLE);
        }

        number.setText(selectedTransfer.number);
        key.setText(selectedTransfer.key);
        template.setText(selectedTransfer.template);
        if((selectedTransfer.flags & Balance.FLAG_FORCE_TRANSFER) != 0) {
            force.setText(getResources().getString(R.string.sms_force));
        } else {
            force.setText(getResources().getString(R.string.sms_noforce));
        }

        //Main.refreshHeader();

    }
}
